How do I parse a header with two different version [ID3] avoiding code duplication?

Posted by user66141 on Programmers See other posts from Programmers or by user66141
Published on 2012-09-25T16:14:58Z Indexed on 2012/10/01 15:52 UTC
Read the original article Hit count: 282

I really hope you can give me some interesting viewpoints for my situation, because I am not satisfied with my current approach.

I am writing an MP3 parser, starting with an ID3v2 parser.

Right now I`m working on the extended header parsing, my issue is that the optional header is defined differently in version 2.3 and 2.4 of the tag.

The 2.3 version optional header is defined as follows:

struct ID3_3_EXTENDED_HEADER{
    DWORD dwExtHeaderSize;          //Extended header size (either 6 or 8 bytes , excluded)
    WORD  wExtFlags;                //Extended header flags
    DWORD dwSizeOfPadding;          //Size of padding (size of the tag excluding the frames and headers)
};

While the 2.4 version is defined :

struct ID3_4_EXTENDED_HEADER{
    DWORD dwExtHeaderSize;          //Extended header size (synchsafe int)
    BYTE  bNumberOfFlagBytes;       //Number of flag bytes
    BYTE  bFlags;                   //Flags
};

How could I parse the header while minimizing code duplication?

Using two different functions to parse each version sounds less great, using a single function with a different flow for each occasion is similar, any good practices for this kind of issues ? Any tips for avoiding code duplication?

Any help would be appreciated.

© Programmers or respective owner

Related posts about programming-practices

Related posts about c